import { rankMgrType } from "./RankMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class homeRankItem extends cc.Component {
    // 假设预制体内有一个 Label 组件用于显示数据
    @property(cc.Label)
    label1: cc.Label = null;
    @property(cc.Label)
    label2: cc.Label = null;
    @property(cc.Label)
    label3: cc.Label = null;

    @property(cc.Label)
    diquName: cc.Label = null;


    public itemID: number = -1;


    // 用于更新内容和id的方法
    public updateContent(data: rankMgrType, id: number): void {
        this.itemID = id;
        let curData = data.cityRankInfo[id];

        this.label1.string = `第${this.numberToChinese(id+1)}名`;
        this.label2.string = `${curData.grade}位晋升大师`;
        this.label3.string = `${curData.university}位晋升雀神`;

        if(id==0){
            this.label1.node.parent.x = 183
            this.label1.node.parent.y = 4
        }else{
            this.label1.node.parent.x = 180
            this.label1.node.parent.y = 12
        }


        let sprite = this.node.getChildByName("sprite");
        if (this.itemID == 0) {
            sprite.children[0].active = true;
            sprite.children[1].active = false;
        }
        else {
            sprite.children[0].active = false;
            sprite.children[1].active = true;
        }
        // ...根据data更新其他数据
        this.renewSp(curData.cityName);


    }

    numberToChinese(num) {
        if (num == 10) {
            return '十'
          } else if (num == 1) {
            return '一'
          }
          const digits = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
          const units = ['', '十', '百', '千', '万'];
          let result = '';
          let numStr = num.toString();
          for (let i = 0; i < numStr.length; i++) {
            let digit = parseInt(numStr.charAt(i));
            let unit = units[numStr.length - i - 1];
            if (digit === 0) {
              // 当前数字为0时不需要输出汉字，但需要考虑上一个数字是否为0，避免出现连续的零
              if (result.charAt(result.length - 1) !== '零') {
                result += '';
              }
            } else {
              result += digits[digit] + unit;
            }
          }
          // 对于一些特殊的数字，如10、100等，需要在最前面加上“一”
          if (result.charAt(0) === '一') {
            result = result.substr(1, result.length);
          } else if (result.charAt(0) === '百') {
            result = '一' + result;
          } else if (result.charAt(0) === '千') {
            result = '一' + result;
          }
          return result;

      }



    /** 根据地区名更新图片 */

    renewSp(name: string) {
        let bundleName = "mainScene";
        let bundle = cc.assetManager.getBundle(bundleName);
        let xueziNode = this.node.getChildByName("xuezi");
        let spNode = xueziNode.getChildByName("sp");
        let xueNode = xueziNode.getChildByName("xue");
        let sprite = spNode.getComponent(cc.Sprite);

        let path = "";
        let isShowXueZi = true;
        // if (name == "内蒙古") {
        //     name = "内蒙古学子";
        //     isShowXueZi = false;
        // }
        // else if (name == "黑龙江") {
        //     name = "黑龙江学子";
        //     isShowXueZi = false;
        // }
        this.diquName.string = name+'馆'
        // path = "image/new/shenfen/" + name;
        // bundle.load(path, cc.SpriteFrame, ((error, assets) => {
        //     if (error) {
        //         cc.error("图片加载失败:" + path);
        //         sprite.spriteFrame = null;
        //         return;
        //     }
        //     sprite.spriteFrame = assets;
        //     if (isShowXueZi) {
        //         xueNode.active = isShowXueZi;
        //         spNode.position = cc.v3(-84, 0, 0);
        //     }
        //     else {
        //         xueNode.active = isShowXueZi;
        //         spNode.position = cc.v3(0, 0, 0);
        //     }
        // }))
    }


}
